<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>父访问子-$childern</title>
</head>
<body>
    <div id="app">
        <cpn ref="cpn"></cpn>
        <button @click="getChildren">按钮</button>
    </div>


    <template id="cpn">
        <div>
            <h2>我是子组件</h2>
        </div>
    </template>

    <script src="../vue/vue.js"></script>
    <script>
        const cpn = {
            template:'#cpn',
            data() {
                return {
                    
                }
            },
            props:{
            },
            methods: {
                showMessage(){
                    console.log('message');                    
                }
            },
        }
        const app = new Vue({
            el:'#app',
            data:{
                
            },
            components:{
                cpn
            },
            methods: {
                getChildren(){
                    //通过$children获取到的是所有的子组件,它是数组
                    //this.$children[0].showMessage();
                    
                    //这种方法用的很多
                    //通过$refs:需要在子组件上加上ref属性值为子组件名字,然后通过$refs.设置的名字获取到相应的子组件
                    this.$refs.cpn.showMessage();
                }
            },
        });
    </script>
</body>
</html>